<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use cmf\controller\RestBaseController;
use cmf\controller\RestUserBaseController;
use cmf\model\UserModel;
use model\OrderModel;
use think\facade\Db;

class CenterController extends RestUserBaseController
{
    protected $userModel;
    public $flag;

    public function initialize()
    {
        parent::initialize();
        $this->userModel = new UserModel();
    }

    /**
     * 订单信息
     * @throws \Exception
     */
    public function index()
    {
        $data = $this->user;
        $waitPay = OrderModel::where(['user_id' => $this->userId, 'status' => 0])->count();
        $waitUsed = OrderModel::where(['user_id' => $this->userId, 'status' => 1])->count();
        $isUsed = OrderModel::where(['user_id' => $this->userId, 'status' => 2])->count();
        $close = OrderModel::where(['user_id' => $this->userId, 'status' => -1])->count();
        $data['wait_pay'] = $waitPay;
        $data['wait_user'] = $waitUsed;
        $data['is_used'] = $isUsed;
        $data['close'] = $close;
        return $this->success('成功', $data);
    }

    /**
     * 更新用户信息
     */
    public function getUserBalance()
    {
        $user = UserModel::where(['id'=>$this->userId])->find()->toArray();
        if (!empty($user)){
            $this->user = $user;
            $this->userId = $user['id'];
        }
        return $this->success('成功', ['balance'=>$user['balance']]);
    }

}
